<template>
  <div class="denglu">
    <hm-header style="background-color: #fff">面经登录</hm-header>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />

      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />

      <div style="margin: 16px">
        <van-button block type="info" native-type="submit" color="#fa6d1d"
          >登录</van-button
        >
      </div>
    </van-form>

    <div class="hm-log-footer">
      <router-link to="/zhuche">注册账号</router-link>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "DengLu",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit() {
      axios({
        url: "http://interview-api-t.itheima.net/h5/user/login",
        method: "POST",
        data: {
          username: this.username,
          password: this.password,
        },
      })
        .then((res) => {
          console.log(res);
          this.$router.push(`/layout/article/recommend?token=${res.data.data.token}`);
          localStorage.setItem("token", res.data.data.token);
        })
        .catch((err) => {
          console.log(err);
          alert("登录失败");
        });
    },
    formatter(value) {
      return value.replace(/[^\d]/g, "");
    },
  },
};
</script>

<style scoped lang="less">
.hm-log-footer {
  text-align: right;
  margin-top: 10px;
  margin-right: 20px;
  a {
    font-size: 12px;
    color: #006699;
  }
}
</style>
